<?php



namespace App\Http\Controllers\Api\Site;


use App\Http\Controllers\Api\BasicController;
use App\Repositories\ResponseCode;
use App\Repositories\ResponseMsg;
use Illuminate\Support\Facades\Storage;
use Jiannei\Response\Laravel\Support\Facades\Response;

class UploadController extends BasicController
{

    /**
     * @api {post} /site/upload/image 上传图片
     * @apiName SiteUploadImage
     * @apiGroup SiteUpload
     *
     */
    public function image()
    {
        $urls = [];
        $extensions = ['gif', 'icon', 'jpg', 'png','mp4','mav','pcm','mp3'];
        foreach ($this->request->file() as $file) {
            if (!in_array($file->extension(), $extensions)) Response::fail(ResponseMsg::FILE_EXT_ERR, ResponseCode::FILE_EXT_ERR);
            $name = substr(md5_file($file), 0, 8) . '.' . $file->extension();
//            $urls[] = $file->storeAs('images/'.date('Y_m'), $name);
            if(in_array($file->extension(),['mav','pcm','mp3'])){
//                $urls[] = Storage::url($file->storeAs('voice/' . date('Y_m'), $name));
                $urls[] = $file->storeAs('voice/' . date('Y_m'), $name);
            }else {
//                $urls[] = Storage::url($file->storeAs('images/' . date('Y_m'), $name));
                $urls[] = $file->storeAs('images/' . date('Y_m'), $name);
            }
        }
        return Response::success($urls);
    }

    /**
     * @api {post} /site/upload/file 上传文件
     * @apiName SiteUploadFile
     * @apiGroup SiteUpload
     *
     */
    public function file()
    {
        $urls = [];
        $extensions = ['doc', 'docx',];
        foreach ($this->request->file() as $file) {
            if (!in_array($file->extension(), $extensions)) Response::fail(ResponseMsg::FILE_EXT_ERR, ResponseCode::FILE_EXT_ERR);
            $name = substr(md5_file($file), 0, 8) . '.' . $file->extension();
            $urls[] = Storage::url($file->storeAs('files', $name));
        }
        return Response::success($urls);
    }

}
